package com.net.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.net.annotaion.ApiLog;
import com.net.controller.model.ResponseModel;
import com.net.controller.vo.EditWebsiteParam;
import com.net.controller.vo.ListPageParam;
import com.net.controller.vo.WebsiteResponseModel;
import com.net.model.Pack;
import com.net.model.Page;
import com.net.model.PageData;
import com.net.model.User;
import com.net.model.Website;
import com.net.service.vo.ServiceCode;
import com.net.service.website.WebsiteService;
import com.net.util.ResponseEntityHelper;

@Controller
@RequestMapping(value = "/net/detect")
public class WebsiteApi extends SuperController {
	WebsiteService websiteService;

	@Resource(name = "websiteService")
	public void setWebsiteService(WebsiteService websiteService) {
		this.websiteService = websiteService;
	}

	@ApiLog
	@RequestMapping(value = "/website/tolistpage")
	public ModelAndView toListPage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("website/website_manage");
		return mv;
	}
	
	@ApiLog
	@RequestMapping(value = "/website/toeditpage")
	public ModelAndView toEditPage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("website/website_edit");
		return mv;
	}


	/*@ApiLog
	@RequestMapping(value = "/pack/listpage", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> listPage(@RequestBody ListPageParam listPageParam, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		Page page = listPageParam.getPage();
		PageData pd = new PageData();
		page.setPd(pd);
		List<Pack> packageList = packageService.listPagePackage(page);
		listPageParam.setPage(page);
		listPageParam.setObjects(packageList);
		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "获取成功！",listPageParam);
	}*/

	@ApiLog
	@RequestMapping(value = "/website/add", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> addWebsite(@RequestBody EditWebsiteParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		String domain = param.getDomain();
		String type = param.getType();
		String name = param.getName();
		if(name == null || "".equals(name)){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输入名称");
		}
		if(domain == null || "".equals(domain)){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输入域名");
		}
		
		if(type == null || "".equals(type)){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输类型");
		}
		ResponseModel res = websiteService.addWebsite(name, domain, type);
		return ResponseEntityHelper.returnResponse(res);
	}

	@ApiLog
	@RequestMapping(value = "/website/listall", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> listPageWebsite(HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		List<Website> websiteList = websiteService.listAll();
		WebsiteResponseModel wrm = new WebsiteResponseModel();
		wrm.setWebsiteList(websiteList);
		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "获取成功",wrm);
	}
	
	@ApiLog
	@RequestMapping(value = "/website/edit", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> editWebsite(@RequestBody EditWebsiteParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(param.getId() == 0 || "".equals(param.getId())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "参数错误");
		}
		ResponseModel res = websiteService.editWebsite(param.getId(), param.getName(), param.getDomain(), param.getType());
		return ResponseEntityHelper.returnResponse(res);
	}
	
	@ApiLog
	@RequestMapping(value = "/website/del", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> delWebsite(@RequestBody EditWebsiteParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(param.getId() == 0 || "".equals(param.getId())){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "参数错误");
		}
		boolean res = websiteService.delWebsite(param.getId());
		
		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "删除成功");
	}
	
	
	
}
